home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / sharing.js < prev    next >
Text File  |  2008-08-20  |  4KB  |  115 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2008
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Atul Varma <varmaa@toolness.com>
  22.  *  Anant Narayanan <anant@kix.in>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. EXPORTED_SYMBOLS = ["Sharing"];
  39.  
  40. const Cc = Components.classes;
  41. const Ci = Components.interfaces;
  42. const Cu = Components.utils;
  43.  
  44. Cu.import("resource://weave/async.js");
  45. Cu.import("resource://weave/identity.js");
  46.  
  47. Function.prototype.async = Async.sugar;
  48.  
  49. function Api(dav) {
  50.   this._dav = dav;
  51. }
  52.  
  53. Api.prototype = {
  54.   shareWithUsers: function Api_shareWithUsers(path, users, folder, onComplete) {
  55.     return this._shareGenerator.async(this,
  56.                                onComplete,
  57.                                path,
  58.                                users, folder);
  59.   },
  60.  
  61.   getShares: function Api_getShares(onComplete) {
  62.     return this._getShareGenerator.async(this, onComplete);
  63.   },
  64.   
  65.   _getShareGenerator: function Api__getShareGenerator() {
  66.     let self = yield;
  67.     let id = ID.get(this._dav.identity);
  68.     
  69.     this._dav.formPost("/api/share/get.php", ("uid=" + escape(id.username) +
  70.                                               "&password=" + escape(id.password)),
  71.                                               self.cb);
  72.     let xhr = yield;
  73.     self.done(xhr.responseText);
  74.   },
  75.  
  76.   _shareGenerator: function Api__shareGenerator(path, users, folder) {
  77.     let self = yield;
  78.     let id = ID.get(this._dav.identity);
  79.  
  80.     let cmd = {"version" : 1,
  81.                "directory" : path,
  82.                "share_to_users" : users};
  83.     let jsonSvc = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
  84.     let json = jsonSvc.encode(cmd);
  85.  
  86.     this._dav.formPost("/api/share/",
  87.                    ("cmd=" + escape(json) +
  88.                     "&uid=" + escape(id.username) +
  89.                     "&password=" + escape(id.password) +
  90.                     "&name=" + escape(folder)),
  91.                    self.cb);
  92.     let xhr = yield;
  93.  
  94.     let retval;
  95.  
  96.     if (xhr.status == 200) {
  97.       if (xhr.responseText == "OK") {
  98.         retval = {wasSuccessful: true};
  99.       } else {
  100.         retval = {wasSuccessful: false,
  101.                   errorText: xhr.responseText};
  102.       }
  103.     } else {
  104.       retval = {wasSuccessful: false,
  105.                 errorText: "Server returned status " + xhr.status};
  106.     }
  107.  
  108.     self.done(retval);
  109.   }
  110. };
  111.  
  112. Sharing = {
  113.   Api: Api
  114. };
  115.